/**
 * <copyright>
 *
 * Copyright (c) 2011 Zeligsoft (2009) Limited
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   Zeligsoft (2009) Limited - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.omg.deployment.components;

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent;
import org.eclipse.emf.eef.runtime.context.PropertiesEditingContext;
import org.eclipse.emf.eef.runtime.impl.components.SinglePartPropertiesEditingComponent;
import org.eclipse.emf.eef.runtime.impl.utils.EEFConverterUtil;
import org.omg.deployment.DeploymentPackage;
import org.omg.deployment.MonolithicDeploymentDescription;
import org.omg.deployment.parts.DeploymentViewsRepository;
import org.omg.deployment.parts.MonolithicDeploymentDescriptionPropertiesEditionPart;
import org.omg.xmi.XmiPackage;


/**
 * @author Toby McClean; Zeligsoft (2009) Limited
 * @generated
 */
public class MonolithicDeploymentDescriptionPropertiesEditionComponent extends SinglePartPropertiesEditingComponent {

	/**
	 * @generated
	 */
	
	public static String BASE_PART = "Base"; //$NON-NLS-1$

	
	
	/**
	 * Default constructor
	 * @generated
	 */
	public MonolithicDeploymentDescriptionPropertiesEditionComponent(PropertiesEditingContext editingContext, EObject monolithicDeploymentDescription, String editing_mode) {
    super(editingContext, monolithicDeploymentDescription, editing_mode);
    parts = new String[] { BASE_PART };
    repositoryKey = DeploymentViewsRepository.class;
    partKey = DeploymentViewsRepository.MonolithicDeploymentDescription.class;
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent#initPart(java.lang.Object, int, org.eclipse.emf.ecore.EObject, 
	 *      org.eclipse.emf.ecore.resource.ResourceSet)
	 * @generated
	 */
	public void initPart(Object key, int kind, EObject elt, ResourceSet allResource) {
    setInitializing(true);
    if (editingPart != null && key == partKey) {
      editingPart.setContext(elt, allResource);
      final MonolithicDeploymentDescription monolithicDeploymentDescription = (MonolithicDeploymentDescription)elt;
      final MonolithicDeploymentDescriptionPropertiesEditionPart basePart = (MonolithicDeploymentDescriptionPropertiesEditionPart)editingPart;
      // init values
      if (monolithicDeploymentDescription.getName() != null && isAccessible(DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.name))
        basePart.setName(EEFConverterUtil.convertToString(XMLTypePackage.eINSTANCE.getString(), monolithicDeploymentDescription.getName()));
      
      if (monolithicDeploymentDescription.getLabel() != null && isAccessible(DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.label))
        basePart.setLabel(EEFConverterUtil.convertToString(XMLTypePackage.eINSTANCE.getString(), monolithicDeploymentDescription.getLabel()));
      
      // FIXME NO VALID CASE INTO template public updater(editionElement : PropertiesEditionElement, view : View, pec : PropertiesEditionComponent) in widgetControl.mtl module, with the values : source, MonolithicDeploymentDescription, MonolithicDeploymentDescription.
      if (monolithicDeploymentDescription.getId() != null && isAccessible(DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.id))
        basePart.setId(EEFConverterUtil.convertToString(XMLTypePackage.eINSTANCE.getString(), monolithicDeploymentDescription.getId()));
      
      // init filters
      
      
      // FIXME NO VALID CASE INTO template public filterUpdater(editionElement : PropertiesEditionElement, view : View, pec : PropertiesEditionComponent) in widgetControl.mtl module, with the values : source, MonolithicDeploymentDescription, MonolithicDeploymentDescription.
      
      // init values for referenced views
      
      // init filters for referenced views
      
    }
    setInitializing(false);
  }







	/**
	 * {@inheritDoc}
	 * @see org.eclipse.emf.eef.runtime.impl.components.StandardPropertiesEditionComponent#associatedFeature(java.lang.Object)
	 */
	public EStructuralFeature associatedFeature(Object editorKey) {
		if (editorKey == DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.name) {
			return DeploymentPackage.eINSTANCE.getMonolithicDeploymentDescription_Name();
		}
		if (editorKey == DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.label) {
			return XmiPackage.eINSTANCE.getXMI_Label();
		}
		if (editorKey == DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.source) {
			return DeploymentPackage.eINSTANCE.getMonolithicDeploymentDescription_Source();
		}
		if (editorKey == DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.id) {
			return XmiPackage.eINSTANCE.getXMI_Id();
		}
		return super.associatedFeature(editorKey);
	}

	/**
	 * {@inheritDoc}
	 * @see org.eclipse.emf.eef.runtime.impl.components.StandardPropertiesEditionComponent#updateSemanticModel(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
	 * @generated
	 */
	public void updateSemanticModel(final IPropertiesEditionEvent event) {
    MonolithicDeploymentDescription monolithicDeploymentDescription = (MonolithicDeploymentDescription)semanticObject;
  }

	/**
	 * {@inheritDoc}
	 * @see org.eclipse.emf.eef.runtime.impl.components.StandardPropertiesEditionComponent#updatePart(org.eclipse.emf.common.notify.Notification)
	 */
	public void updatePart(Notification msg) {
		if (editingPart.isVisible()) {	
			MonolithicDeploymentDescriptionPropertiesEditionPart basePart = (MonolithicDeploymentDescriptionPropertiesEditionPart)editingPart;
			if (DeploymentPackage.eINSTANCE.getMonolithicDeploymentDescription_Name().equals(msg.getFeature()) && basePart != null && isAccessible(DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.name)) {
				if (msg.getNewValue() != null) {
					basePart.setName(EcoreUtil.convertToString(XMLTypePackage.eINSTANCE.getString(), msg.getNewValue()));
				} else {
					basePart.setName("");
				}
			}
			if (XmiPackage.eINSTANCE.getXMI_Label().equals(msg.getFeature()) && basePart != null && isAccessible(DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.label)) {
				if (msg.getNewValue() != null) {
					basePart.setLabel(EcoreUtil.convertToString(XMLTypePackage.eINSTANCE.getString(), msg.getNewValue()));
				} else {
					basePart.setLabel("");
				}
			}
			// FIXME INVALID CASE INTO template public liveUpdater(editionElement : PropertiesEditionElement, view : View, pec : PropertiesEditionComponent) in widgetControl.mtl module, with the values : source, MonolithicDeploymentDescription, MonolithicDeploymentDescription.
			if (XmiPackage.eINSTANCE.getXMI_Id().equals(msg.getFeature()) && basePart != null && isAccessible(DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.id)) {
				if (msg.getNewValue() != null) {
					basePart.setId(EcoreUtil.convertToString(XMLTypePackage.eINSTANCE.getString(), msg.getNewValue()));
				} else {
					basePart.setId("");
				}
			}
			
		}
	}


	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent#validateValue(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
	 * @generated
	 */
	public Diagnostic validateValue(IPropertiesEditionEvent event) {
    Diagnostic ret = Diagnostic.OK_INSTANCE;
    if (event.getNewValue() != null) {
      try {
        if (DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.name == event.getAffectedEditor()) {
          Object newValue = event.getNewValue();
          if (newValue instanceof String) {
            newValue = EcoreUtil.createFromString(DeploymentPackage.eINSTANCE.getMonolithicDeploymentDescription_Name().getEAttributeType(), (String)newValue);
          }
          ret = Diagnostician.INSTANCE.validate(DeploymentPackage.eINSTANCE.getMonolithicDeploymentDescription_Name().getEAttributeType(), newValue);
        }
        if (DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.label == event.getAffectedEditor()) {
          Object newValue = event.getNewValue();
          if (newValue instanceof String) {
            newValue = EcoreUtil.createFromString(XmiPackage.eINSTANCE.getXMI_Label().getEAttributeType(), (String)newValue);
          }
          ret = Diagnostician.INSTANCE.validate(XmiPackage.eINSTANCE.getXMI_Label().getEAttributeType(), newValue);
        }
        if (DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.source == event.getAffectedEditor()) {
          BasicDiagnostic chain = new BasicDiagnostic();
          for (Iterator iterator = ((List)event.getNewValue()).iterator(); iterator.hasNext();) {
            chain.add(Diagnostician.INSTANCE.validate(DeploymentPackage.eINSTANCE.getMonolithicDeploymentDescription_Source().getEAttributeType(), iterator.next()));
          }
          ret = chain;
        }
        if (DeploymentViewsRepository.MonolithicDeploymentDescription.Properties.id == event.getAffectedEditor()) {
          Object newValue = event.getNewValue();
          if (newValue instanceof String) {
            newValue = EcoreUtil.createFromString(XmiPackage.eINSTANCE.getXMI_Id().getEAttributeType(), (String)newValue);
          }
          ret = Diagnostician.INSTANCE.validate(XmiPackage.eINSTANCE.getXMI_Id().getEAttributeType(), newValue);
        }
      } catch (IllegalArgumentException iae) {
        ret = BasicDiagnostic.toDiagnostic(iae);
      } catch (WrappedException we) {
        ret = BasicDiagnostic.toDiagnostic(we);
      }
    }
    return ret;
  }

}
